﻿using CSharpLibs.Annotations;

namespace System.IO
{
	[Builtin]
	public class Path
	{
		public static readonly char AltDirectorySeparatorChar = '/';
		public static readonly char DirectorySeparatorChar = '\\';
		public static readonly char VolumeSeparatorChar = ':';
		
		public static string Dot;
		public static string DirectorySeparatorString;

		static Path()
		{
			char[] da = new char[1];
			da[0] = '.';
			Dot = new String(da);

			char[] ds = new char[1];
			ds[0] = '\\';
			DirectorySeparatorString = new String(ds);
		}


		public static string ChangeExtension(string path, string extension)
		{
			if(path == null)
				return null;
			
			String str = path;
			int length = path.Length;
			while(--length >= 0)
			{
				char ch = path[length];
				if(ch == '.')
				{
					str = path.Substring(0, length);
					break;
				}
				if(((ch == DirectorySeparatorChar) || (ch == AltDirectorySeparatorChar)) || (ch == VolumeSeparatorChar))
				{
					break;
				}
			}

			if((extension == null) || (path.Length == 0))
				return str;
			
			if((extension.Length == 0) || (extension[0] != '.'))
				str = String.Concat(str, Dot);
			
			return String.Concat(str, extension);
		}

		public static string Combine(string path1, string path2)
		{
			if(path2.Length == 0)
				return path1;
			
			if(path1.Length == 0)
				return path2;
			
			if(IsPathRooted(path2))
				return path2;
			
			char ch = path1[path1.Length - 1];
			if(((ch != DirectorySeparatorChar) && (ch != AltDirectorySeparatorChar)) && (ch != VolumeSeparatorChar))
				return String.Concat(path1, DirectorySeparatorString, path2);

			return String.Concat(path1, path2);
		}

		public static string GetDirectoryName(string path)
		{
			if(path != null)
			{
				path = NormalizePath(path, false);
				int rootLength = GetRootLength(path);
				if(path.Length > rootLength)
				{
					int length = path.Length;
					if(length == rootLength)
					{
						return null;
					}
					while(((length > rootLength) && (path[--length] != DirectorySeparatorChar)) && (path[length] != AltDirectorySeparatorChar))
					{
						;
					}
					return path.Substring(0, length);
				}
			}
			return null;

		}

		public static string GetExtension(string path)
		{
			if(path == null)
			{
				return null;
			}
			
			int length = path.Length;
			int startIndex = length;
			while(--startIndex >= 0)
			{
				char ch = path[startIndex];
				if(ch == '.')
				{
					if(startIndex != (length - 1))
					{
						return path.Substring(startIndex, length - startIndex);
					}
					return String.Empty;
				}
				if(((ch == DirectorySeparatorChar) || (ch == AltDirectorySeparatorChar)) || (ch == VolumeSeparatorChar))
				{
					break;
				}
			}
			return String.Empty;

		}

		public static string GetFileName(string path)
		{
			if(path != null)
			{
				int length = path.Length;
				int num2 = length;
				while(--num2 >= 0)
				{
					char ch = path[num2];
					if(((ch == DirectorySeparatorChar) || (ch == AltDirectorySeparatorChar)) || (ch == VolumeSeparatorChar))
					{
						return path.Substring(num2 + 1, (length - num2) - 1);
					}
				}
			}
			return path;
		}

		public static string GetFullPath(string path)
		{ return NormalizePath(path, true); }

		internal static int GetRootLength(string path)
		{
			int num = 0;
			int length = path.Length;
			if((length >= 1) && IsDirectorySeparator(path[0]))
			{
				num = 1;
				if((length >= 2) && IsDirectorySeparator(path[1]))
				{
					num = 2;
					int num3 = 2;
					while((num < length) && (((path[num] != DirectorySeparatorChar) && (path[num] != AltDirectorySeparatorChar)) || (--num3 > 0)))
					{
						num++;
					}
				}
				return num;
			}
			if((length >= 2) && (path[1] == VolumeSeparatorChar))
			{
				num = 2;
				if((length >= 3) && IsDirectorySeparator(path[2]))
				{
					num++;
				}
			}
			return num;
		}

		internal static bool IsDirectorySeparator(char c)
		{
			if(c != DirectorySeparatorChar)
			{
				return (c == AltDirectorySeparatorChar);
			}
			return true;
		}

		public static bool IsPathRooted(string path)
		{
			if(path != null)
			{
				int length = path.Length;
				if(((length >= 1) && ((path[0] == DirectorySeparatorChar) || (path[0] == AltDirectorySeparatorChar))) || ((length >= 2) && (path[1] == VolumeSeparatorChar)))
				{
					return true;
				}
			}
			return false;
		}

		[BuiltinMethod, BuiltinSemantics("path_normalize")]
		internal static string NormalizePath(string path, bool fullCheck)
		{ return null; }
	}
}
